#include "ruleBuffer.h"

#include <string.h>

/**
*
* 初始化环形缓冲，如果初始化成功则
*
*
**/
int8_t RingBuf_Init(RingBuff_t* pRingBuff)
{
	if(NULL==pRingBuff)
	{
		return ERROR;
	}
	
	pRingBuff->Head=0;
	pRingBuff->Tail=0;
	pRingBuff->Lenght=0;
	pRingBuff->totalLenght=RINGBUFFE_LEN;
	
	return OK;
}

int8_t RingBuf_Btye_Wrie(RingBuff_t* pRingBuff, uint8_t data)
{
	if(NULL==pRingBuff)
	{
		return ERROR;
	}
	if(pRingBuff->Lenght>=RINGBUFFE_LEN)
	{
		return BUFFER_FULL_SIZE;
	}
	
	pRingBuff->Ring_Buff[pRingBuff->Tail]=data;
	pRingBuff->Tail=(pRingBuff->Tail+1)%pRingBuff->totalLenght;
	pRingBuff->Lenght++;
	
	return 1;
}

int8_t RingBuf_Byte_Read(RingBuff_t* pRingBuff, uint8_t* pDatas, uint8_t len)
{
	if(NULL==pRingBuff)
	{
		return ERROR;
	}
	
	if(pRingBuff->Lenght<len)
	{
		return LESS_LENGHT;
	}
	
	for(int i=0; i<len;i++)
	{
		pDatas[i] = pRingBuff->Ring_Buff[pRingBuff->Head];
		
		pRingBuff->Head=(pRingBuff->Head+1)%pRingBuff->totalLenght;
		pRingBuff->Lenght--;
	}
	
	return len;
}
